//
//  ALPCustomHistoryViewController.m
//  AlpFM
//
//  Created by KING on 16/1/28.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPCustomHistoryViewController.h"
#import "ALPCustomHistoryTableViewCell.h"
#import "ALPPlayViewController.h"
@interface ALPCustomHistoryViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, retain) NSMutableArray *allArray;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *holdLabel;

@end

@implementation ALPCustomHistoryViewController
#pragma mark - 将要出现的时候就初始化数组
- (void)viewWillAppear:(BOOL)animated{
    self.allArray = [[ALPDateBaseManager shareFMDB] seleteHistory];
    //    NSLog(@"数组的个数%ld", self.allArray.count);
    [self.myTableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100-40) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];

    [self.myTableView registerClass:[ALPCustomHistoryTableViewCell class] forCellReuseIdentifier:@"history"];
    //    self.myTableView.bounces = NO;
    //    self.myTableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //清空按钮
    [self p_headerView];
    
    //占位字
    self.holdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH *0.13)];
    self.holdLabel.center = CGPointMake(SCREEN_WIDTH *0.5, SCREEN_WIDTH *0.5);
    self.holdLabel.text = @"~ 您还没有听过节目哦 ~";
    self.holdLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.holdLabel];

}
#pragma mark - 清空按钮的创建
- (void)p_headerView{
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    //    imageView.backgroundColor = CLEAR;
    imageView.userInteractionEnabled = YES;
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    [self.button setTitle:@"清空记录" forState:UIControlStateNormal];
    self.button.titleLabel.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
    self.button.backgroundColor = [UIColor clearColor];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
//        [self.button setTitleColor:WHITE forState:UIControlStateNormal];
//    } else {
//        [self.button setTitleColor:BLACK forState:UIControlStateNormal];
//    }
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview: self.button];
    self.myTableView.tableHeaderView = imageView;
}

#pragma mark - 清空按钮的执行
- (void)buttonAction:(UIButton *)btn{
    NSLog(@"清空按钮");
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
//        [self.button setTitleColor:WHITE forState:UIControlStateNormal];
//    } else {
//        [self.button setTitleColor:BLACK forState:UIControlStateNormal];
//    }
    
    [[ALPDateBaseManager shareFMDB] deleteHistory];
    [self.allArray removeAllObjects];
    [self.myTableView reloadData];
    
}

#pragma mark - 协议方法 利用刷新组 来刷新holdLabel
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.allArray.count != 0) {
        self.holdLabel.alpha = 0;
    } else {
        self.holdLabel.alpha = 1;
    }
    return 1;
}

#pragma mark - 协议方法 高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 0.267*SCREEN_WIDTH;
}

#pragma mark - 协议方法 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allArray.count;
}

#pragma mark - 协议方法 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ALPCustomHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"history"];
    cell.model = [self.allArray objectAtIndex: indexPath.row];
    return cell;
}

#pragma mark - 选中执行 点击直接放歌
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    ALPPlayViewController *player = [ALPPlayViewController sharePlayerViewController];
//    UINavigationController *navc = [[HFBaseNavigationController alloc] initWithRootViewController:player];
    ALPHistoryModel *model = [self.allArray objectAtIndex:indexPath.row];
    player.song_ID = model.song_ID.intValue;      //歌曲的ID
    player.indexRow = indexPath.row;     //点击的歌曲的下标
    
    //***************** 数组转换 ****************
    NSMutableArray *array = [NSMutableArray array];
    for (ALPHistoryModel *lisionModel in self.allArray) {
        
        ALPSingleModel *listModel = [[ALPSingleModel alloc] init];
        //        NSLog(@"AAA%@", lisionModel.trackID);
        listModel.trackId = [NSString stringWithFormat:@"%@", lisionModel.song_ID];
        listModel.title = lisionModel.title;
        listModel.playtimes = @"13256";
        listModel.duration = @"1212";
        listModel.comments = @"暂无数据";
        
        [array addObject:listModel];
        
    }
    player.allMusicArray = array;//歌曲数组赋值
    // ******************************************
    
    [self presentViewController:player animated:YES completion:^{
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
